package com.yanghs.shiro.session;

import com.yanghs.common.dao.RedisDao;
import com.yanghs.common.util.SerializeUtil;
import org.apache.shiro.session.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.Serializable;
import java.util.Collection;
import java.util.Deque;

/**
 * @author yanghs
 * @Description:session缓存统一管理
 * @date 2018/3/22 11:45
 */
public class RedisRepository implements IRedisRepository {
    private static final Logger log = LoggerFactory.getLogger(RedisRepository.class);
    public static final String LAB_SESSION_PREFIX = "lab_session:";
    public static final String LAB_SESSION_ALL = "*lab_session:*";


    private RedisDao redisDao;
    public void saveSession(Session session) {
        if(session==null){
            throw new NullPointerException("session is null");
        }
        byte[] key = (LAB_SESSION_PREFIX+session.getId()).toString().getBytes();
        byte[] value =  SerializeUtil.serialize(session);
        try {
            getRedisDao().saveValueByKey(key,value,-1);
            log.debug("session:"+session.getId()+"保存成功，key为："+key.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public Session getSession(Serializable sessionId) {
        if(sessionId == null){
            throw new NullPointerException("sessionId is null");
        }
        byte[] key = (LAB_SESSION_PREFIX+sessionId).getBytes();
        try {
            byte[] value =  getRedisDao().getValueByKey(key);
            log.debug("session:"+sessionId+"获取成功，key为："+key.toString());
            return SerializeUtil.deserialize(value,Session.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    public void deleteSession(Serializable sessionId) {
        if(sessionId == null){
            throw new NullPointerException("sessionId is null");
        }
        byte[] key = (LAB_SESSION_PREFIX+sessionId).getBytes();
        try {
            getRedisDao().deleteByKey(key);
            log.debug("session:"+sessionId+"删除成功，key为："+key.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public Collection<Session> getAllSession() {
        return getRedisDao().getLikeKeys(LAB_SESSION_ALL,Session.class);
    }



    public Deque<Serializable> getAccountSessions(String account) {
        if(account == null){
            throw new NullPointerException("得到账户登录信息失败 account为null");
        }
        try {
            Deque<Serializable> deque = SerializeUtil.deserialize(redisDao.getValueByKey(account.getBytes()),Deque.class);
            String dequestr = deque == null?"" : deque.toString();
            log.debug("得到账户登录信息:"+account+"获取成功"+dequestr);
            return deque;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    public void saveAccountSessions(String account, Deque<Serializable> deque) {
        if(account ==null || deque ==null){
            throw new NullPointerException("缓存账号登录信息失败，null值");
        }
        try {
            redisDao.saveValueByKey(account.getBytes(),SerializeUtil.serialize(deque),-1);
            log.debug("保存账户登录信息:"+account+"获取成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public void deleteAccountSessions(String account) {
        if(account == null){
            throw new NullPointerException("删除账户登录信息失败，account为null");
        }
        try {
            redisDao.deleteByKey(account.getBytes());
            log.debug("删除账户登录信息:"+account+"获取成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public RedisDao getRedisDao() {
        return redisDao;
    }

    public void setRedisDao(RedisDao redisDao) {
        this.redisDao = redisDao;
    }

}
